package com.example.reptile.book.platform.fanqie;

/**
 * @ClassName Test
 * @Description TODO
 * @Author hf
 * @Date 2022/12/26 10:16
 * @Version 1.0
 */

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class LoLlmjjx {
    private static final Map<Integer, Character> map = new HashMap<>();

    public static void main(String[] args) throws IOException {
        initMap();
        //设置下载文件存放磁盘的位置
        File file = new File("C:\\Users\\Administrator\\Desktop\\yn_ry");
        //判断文件夹是否存在，不存在就创建
        if (!file.exists()) {
            file.mkdirs();
        }
        //规定要爬取的网页
        String url = "https://fanqienovel.com/page/7293112336162753576";
        Connection conn = Jsoup.connect(url);
        //伪装成为浏览器，有的网站爬取数据会阻止访问，伪装成浏览器可以访问，这里我伪装成Google浏览器
        Document doc = conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36").get();
        //寻找名叫panel-body的div，接着再在里面寻找叫list-group.list-charts的ul，再在里面寻找li，再在li里寻找a
        Elements as = doc.getElementsByClass("page-directory-content").select("a");
        for (int i = 0; i < 10; i++) {
            //对于每一个元素取出拿到href和小说名字
            String href = as.get(i).attr("href");
            String title = as.get(i).text();
            //file:表示存放文件的位置
            //href:表示要读取内容的页面
            //title:表示存放文件名称
            save(file, href, title);
        }

    }


    private static void save(File file, String href, String title) throws IOException {
        //在file目录下创建每一个章命名的txt文件
        File f = new File(file, title + ".txt");
        //构建输出流对象，因为小说的内容是字符类型的数据
        Writer out = new FileWriter(f);
        //构建读取页面的url
        String url = "https://fanqienovel.com" + href;
        Connection conn = Jsoup.connect(url);
        //伪装成为浏览器
        Document doc = conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36").get();
        //获取小说的正文
        String content = doc.getElementsByClass("muye-reader-content noselect").select("p").html();
        //处理特殊数据
        content = content.replace("<br>", "");
        content = content.replace("&nbsp;", "");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < content.length(); i++) {
            sb.append(parseFont(content.charAt(i)));
        }

        out.write(sb.toString());
        out.close();
        //需要使用休眠，为了防止网站检测为蓄意攻击，停止我们的IP访问

        int n = (int) (Math.random() * 1000 + 100);
        try {
            Thread.sleep(n);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static char parseFont(char c) {
        int cUnicode = c;
        System.out.println(c +" "+cUnicode+" "+map.get(cUnicode));
//        if(map.get(cUnicode) == null)
//        {
//            return c;
//        }else
//        {
//            return map.get(cUnicode);
//        }
        return StringUtils.isEmpty(map.get(cUnicode)) ? c : map.get(cUnicode);
    }


    private static void initMap() {
        map.put(58670, '0');
        map.put(58413, '1');
        map.put(58678, '2');
        map.put(58371, '3');
        map.put(58353, '4');
        map.put(58480, '5');
        map.put(58359, '6');
        map.put(58449, '7');
        map.put(58540, '8');
        map.put(58692, '9');
        map.put(58712, 'a');
        map.put(58542, 'b');
        map.put(58575, 'c');
        map.put(58626, 'd');
        map.put(58691, 'e');
        map.put(58561, 'f');
        map.put(58362, 'g');
        map.put(58619, 'h');
        map.put(58430, 'i');
        map.put(58531, 'j');
        map.put(58588, 'k');
        map.put(58440, 'l');
        map.put(58681, 'm');
        map.put(58631, 'n');
        map.put(58376, 'o');
        map.put(58429, 'p');
        map.put(58555, 'q');
        map.put(58498, 'r');
        map.put(58518, 's');
        map.put(58453, 't');
        map.put(58397, 'u');
        map.put(58356, 'v');
        map.put(58435, 'w');
        map.put(58514, 'x');
        map.put(58482, 'y');
        map.put(58529, 'z');
        map.put(58515, 'A');
        map.put(58688, 'B');
        map.put(58709, 'C');
        map.put(58344, 'D');
        map.put(58656, 'E');
        map.put(58381, 'F');
        map.put(58576, 'G');
        map.put(58516, 'H');
        map.put(58463, 'I');
        map.put(58649, 'J');
        map.put(58571, 'K');
        map.put(58558, 'L');
        map.put(58433, 'M');
        map.put(58517, 'N');
        map.put(58387, 'O');
        map.put(58687, 'P');
        map.put(58537, 'Q');
        map.put(58541, 'R');
        map.put(58458, 'S');
        map.put(58390, 'T');
        map.put(58466, 'U');
        map.put(58386, 'V');
        map.put(58697, 'W');
        map.put(58519, 'X');
        map.put(58511, 'Y');
        map.put(58634, 'Z');
        map.put(58611, '的');
        map.put(58590, '一');
        map.put(58398, '是');
        map.put(58422, '了');
        map.put(58657, '我');
        map.put(58666, '不');
        map.put(58562, '人');
        map.put(58345, '在');
        map.put(58510, '他');
        map.put(58496, '有');
        map.put(58654, '这');
        map.put(58441, '个');
        map.put(58493, '上');
        map.put(58714, '们');
        map.put(58618, '来');
        map.put(58528, '到');
        map.put(58403, '大');
        map.put(58461, '地');
        map.put(58481, '为');
        map.put(58700, '子');
        map.put(58708, '中');
        map.put(58503, '你');
        map.put(58442, '说');
        map.put(58639, '生');
        map.put(58506, '国');
        map.put(58663, '年');
        map.put(58436, '着');
        map.put(58563, '就');
        map.put(58391, '那');
        map.put(58357, '和');
        map.put(58354, '要');
        map.put(58695, '她');
        map.put(58372, '出');
        map.put(58696, '也');
        map.put(58551, '得');
        map.put(58445, '里');
        map.put(58408, '后');
        map.put(58599, '自');
        map.put(58424, '以');
        map.put(58394, '会');
        map.put(58348, '家');
        map.put(58426, '可');
        map.put(58673, '下');
        map.put(58417, '而');
        map.put(58556, '过');
        map.put(58603, '天');
        map.put(58565, '去');
        map.put(58604, '能');
        map.put(58522, '对');
        map.put(58632, '小');
        map.put(58622, '多');
        map.put(58350, '然');
        map.put(58605, '于');
        map.put(58617, '心');
        map.put(58401, '学');
        map.put(58637, '么');
        map.put(58684, '之');
        map.put(58382, '都');
        map.put(58464, '好');
        map.put(58487, '看');
        map.put(58693, '起');
        map.put(58608, '发');
        map.put(58392, '当');
        map.put(58474, '没');
        map.put(58601, '成');
        map.put(58355, '只');
        map.put(58573, '如');
        map.put(58499, '事');
        map.put(58469, '把');
        map.put(58361, '还');
        map.put(58698, '用');
        map.put(58489, '第');
        map.put(58711, '样');
        map.put(58457, '道');
        map.put(58635, '想');
        map.put(58492, '作');
        map.put(58647, '种');
        map.put(58623, '开');
        map.put(58521, '美');
        map.put(58609, '总');
        map.put(58530, '从');
        map.put(58665, '无');
        map.put(58652, '情');
        map.put(58676, '己');
        map.put(58456, '面');
        map.put(58581, '最');
        map.put(58509, '女');
        map.put(58488, '但');
        map.put(58363, '现');
        map.put(58685, '前');
        map.put(58396, '些');
        map.put(58523, '所');
        map.put(58471, '同');
        map.put(58485, '日');
        map.put(58613, '手');
        map.put(58533, '又');
        map.put(58589, '行');
        map.put(58527, '意');
        map.put(58593, '动');
        map.put(58699, '方');
        map.put(58707, '期');
        map.put(58414, '它');
        map.put(58596, '头');
        map.put(58570, '经');
        map.put(58660, '长');
        map.put(58364, '儿');
        map.put(58526, '回');
        map.put(58501, '位');
        map.put(58638, '分');
        map.put(58404, '爱');
        map.put(58677, '老');
        map.put(58535, '因');
        map.put(58629, '很');
        map.put(58577, '绘');
        map.put(58606, '多');
        map.put(58497, '法');
        map.put(58662, '间');
        map.put(58479, '斯');
        map.put(58532, '知');
        map.put(58380, '世');
        map.put(58385, '什');
        map.put(58405, '两');
        map.put(58644, '次');
        map.put(58578, '使');
        map.put(58505, '身');
        map.put(58564, '者');
        map.put(58412, '被');
        map.put(58686, '高');
        map.put(58624, '已');
        map.put(58667, '亲');
        map.put(58607, '其');
        map.put(58616, '进');
        map.put(58368, '此');
        map.put(58427, '话');
        map.put(58423, '常');
        map.put(58633, '与');
        map.put(58525, '活');
        map.put(58543, '正');
        map.put(58418, '感');
        map.put(58597, '见');
        map.put(58683, '明');
        map.put(58507, '问');
        map.put(58621, '力');
        map.put(58703, '理');
        map.put(58438, '尔');
        map.put(58536, '点');
        map.put(58384, '文');
        map.put(58484, '几');
        map.put(58539, '定');
        map.put(58554, '本');
        map.put(58421, '公');
        map.put(58347, '特');
        map.put(58569, '做');
        map.put(58710, '外');
        map.put(58574, '孩');
        map.put(58375, '相');
        map.put(58645, '西');
        map.put(58592, '果');
        map.put(58572, '走');
        map.put(58388, '将');
        map.put(58370, '月');
        map.put(58399, '十');
        map.put(58651, '实');
        map.put(58546, '向');
        map.put(58504, '声');
        map.put(58419, '车');
        map.put(58407, '全');
        map.put(58672, '信');
        map.put(58675, '重');
        map.put(58538, '三');
        map.put(58465, '机');
        map.put(58374, '工');
        map.put(58579, '物');
        map.put(58402, '气');
        map.put(58702, '每');
        map.put(58553, '并');
        map.put(58360, '别');
        map.put(58389, '真');
        map.put(58560, '打');
        map.put(58690, '太');
        map.put(58473, '新');
        map.put(58512, '比');
        map.put(58653, '才');
        map.put(58704, '便');
        map.put(58545, '夫');
        map.put(58641, '再');
        map.put(58475, '书');
        map.put(58583, '部');
        map.put(58472, '水');
        map.put(58478, '像');
        map.put(58664, '眼');
        map.put(58586, '等');
        map.put(58568, '体');
        map.put(58674, '却');
        map.put(58490, '加');
        map.put(58476, '电');
        map.put(58346, '主');
        map.put(58630, '界');
        map.put(58595, '门');
        map.put(58502, '利');
        map.put(58713, '海');
        map.put(58587, '受');
        map.put(58548, '听');
        map.put(58351, '表');
        map.put(58547, '德');
        map.put(58443, '少');
        map.put(58460, '克');
        map.put(58636, '代');
        map.put(58585, '员');
        map.put(58625, '许');
        map.put(58694, '稜');
        map.put(58428, '先');
        map.put(58640, '口');
        map.put(58628, '由');
        map.put(58612, '死');
        map.put(58446, '安');
        map.put(58468, '写');
        map.put(58410, '性');
        map.put(58508, '马');
        map.put(58594, '光');
        map.put(58483, '白');
        map.put(58544, '或');
        map.put(58495, '住');
        map.put(58450, '难');
        map.put(58643, '望');
        map.put(58486, '教');
        map.put(58406, '命');
        map.put(58447, '花');
        map.put(58669, '结');
        map.put(58415, '乐');
        map.put(58444, '色');
        map.put(58549, '更');
        map.put(58494, '拉');
        map.put(58409, '东');
        map.put(58658, '神');
        map.put(58557, '记');
        map.put(58602, '处');
        map.put(58559, '让');
        map.put(58610, '母');
        map.put(58513, '父');
        map.put(58500, '应');
        map.put(58378, '直');
        map.put(58680, '字');
        map.put(58352, '场');
        map.put(58383, '平');
        map.put(58454, '报');
        map.put(58671, '友');
        map.put(58668, '关');
        map.put(58452, '放');
        map.put(58627, '至');
        map.put(58400, '张');
        map.put(58455, '认');
        map.put(58416, '接');
        map.put(58552, '告');
        map.put(58614, '入');
        map.put(58582, '笑');
        map.put(58534, '内');
        map.put(58701, '英');
        map.put(58349, '军');
        map.put(58491, '候');
        map.put(58467, '民');
        map.put(58365, '岁');
        map.put(58598, '往');
        map.put(58425, '何');
        map.put(58462, '度');
        map.put(58420, '山');
        map.put(58661, '觉');
        map.put(58615, '路');
        map.put(58648, '带');
        map.put(58470, '万');
        map.put(58377, '男');
        map.put(58520, '边');
        map.put(58646, '风');
        map.put(58600, '解');
        map.put(58431, '叫');
        map.put(58715, '任');
        map.put(58524, '金');
        map.put(58439, '快');
        map.put(58566, '原');
        map.put(58477, '吃');
        map.put(58642, '妈');
        map.put(58437, '变');
        map.put(58411, '通');
        map.put(58451, '师');
        map.put(58395, '立');
        map.put(58369, '象');
        map.put(58706, '数');
        map.put(58705, '四');
        map.put(58379, '失');
        map.put(58567, '满');
        map.put(58373, '战');
        map.put(58448, '远');
        map.put(58659, '格');
        map.put(58434, '士');
        map.put(58679, '音');
        map.put(58432, '轻');
        map.put(58689, '目');
        map.put(58591, '条');
        map.put(58682, '呢');
        map.put(58620, '时');

    }


}

